<?php
declare (strict_types=1);

namespace app\index\controller;

use think\facade\View;
use think\response\Json;
use app\common\model\Message as MessageModel;
use think\exception\ValidateException;

class Message extends Base
{
    public function index()
    {
        return View::fetch();
    }

    public function add()
    {
        return View::fetch();
    }

    public function getList(): Json
    {
        $param = $this->request->param();
        $map = ['uid' => $this->user->id, 'is_del' => 0];

        return json(MessageModel::getPageList($map, '*', $param['page'], 20, false));
    }

    public function save(): Json
    {
        $param = $this->request->post('', '', $this->filter);
        try {
            $this->validate($param, 'Message');
        } catch (ValidateException $e) {
            return json(['code' => -1, 'msg' => $e->getError()]);
        }

        return json(MessageModel::add($param, $this->user));
    }
}